/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.Dialog;

import controllers.entity.GiaoTrinh;
import controllers.process.CanBoProcess;
import controllers.process.ChamThiProcess;
import controllers.process.GiaoTrinhProcess;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import models.Constant;
import views.JPanel.TTCanBo;

/**
 *
 * @author HoaDuyen
 */
public class DlgThemGiaoTrinh extends java.awt.Dialog {

    private CanBoProcess canBoProcess;
    private GiaoTrinhProcess giaoTrinhProcess;
    private int macanbo;
    private GiaoTrinh giaotrinh;
    private TTCanBo parent;
    public DlgThemGiaoTrinh(JFrame parent, boolean modal,int macanbo,GiaoTrinh giaotrinh ) {
        super(parent, modal);
        this.parent=(TTCanBo)parent;
        this.giaotrinh=giaotrinh;
        this.setLocation(Constant.LOCATE_XDLG, Constant.LOCATE_YDLG);
        this.macanbo=macanbo;
        this.setResizable(false);
        canBoProcess=new CanBoProcess();
        giaoTrinhProcess=new GiaoTrinhProcess();
        initComponents();
        ok.setText("Thêm");
        if (giaotrinh != null) { // Dang o che do sua
            tengiaotrinh.setText(giaotrinh.getTenGiaoTrinh());
            tentacgia.setText(giaotrinh.getTenTacGia());
           //tentacgia.setEditable(false);
            namxuatban.setText(""+giaotrinh.getYear());
            nhaxuatban.setText(giaotrinh.getNhaXuatBan());
            txtGiaoTrinh.setText("Sửa Thông Tin Giáo Trình");
            ok.setText("Lưu");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtGiaoTrinh = new javax.swing.JLabel();
        jPanel23 = new javax.swing.JPanel();
        jLabel50 = new javax.swing.JLabel();
        jLabel51 = new javax.swing.JLabel();
        tentacgia = new javax.swing.JTextField();
        tengiaotrinh = new javax.swing.JTextField();
        jPanel24 = new javax.swing.JPanel();
        jLabel56 = new javax.swing.JLabel();
        jLabel57 = new javax.swing.JLabel();
        namxuatban = new javax.swing.JTextField();
        nhaxuatban = new javax.swing.JTextField();
        ok = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        txtGiaoTrinh.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        txtGiaoTrinh.setText("Thêm Giáo Trình");

        jLabel50.setText("Tên tác giả");

        jLabel51.setText("Tên giáo trình");

        javax.swing.GroupLayout jPanel23Layout = new javax.swing.GroupLayout(jPanel23);
        jPanel23.setLayout(jPanel23Layout);
        jPanel23Layout.setHorizontalGroup(
            jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel23Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel50)
                    .addComponent(jLabel51))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tentacgia)
                    .addComponent(tengiaotrinh, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                .addGap(18, 18, 18))
        );
        jPanel23Layout.setVerticalGroup(
            jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel23Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel50)
                    .addComponent(tentacgia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel51)
                    .addComponent(tengiaotrinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(44, Short.MAX_VALUE))
        );

        jLabel56.setText("Năm xuất bản");

        jLabel57.setText("Nhà xuất bản");

        javax.swing.GroupLayout jPanel24Layout = new javax.swing.GroupLayout(jPanel24);
        jPanel24.setLayout(jPanel24Layout);
        jPanel24Layout.setHorizontalGroup(
            jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel24Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel56)
                    .addComponent(jLabel57))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                .addGroup(jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(namxuatban)
                    .addComponent(nhaxuatban, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                .addGap(18, 18, 18))
        );
        jPanel24Layout.setVerticalGroup(
            jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel24Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(namxuatban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel56))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel24Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(nhaxuatban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel57))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_add.png"))); // NOI18N
        ok.setText("Thêm");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        Cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_delete.png"))); // NOI18N
        Cancel.setText("Hủy");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addComponent(txtGiaoTrinh))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel23, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jPanel24, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(184, 184, 184)
                        .addComponent(ok)
                        .addGap(18, 18, 18)
                        .addComponent(Cancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(txtGiaoTrinh)
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel24, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel23, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(Cancel))
                .addGap(32, 32, 32))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        if (tentacgia.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tentacgia.selectAll();
            tentacgia.requestFocus();
            return;
        } 
         if (tengiaotrinh.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            tengiaotrinh.selectAll();
            tengiaotrinh.requestFocus();
            return;
        } 
        if (nhaxuatban.getText().length()>40){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            nhaxuatban.selectAll();
            nhaxuatban.requestFocus();
            return;
        } 
        if (tentacgia.getText().equals("") ||
            tengiaotrinh.getText().equals("")||
            nhaxuatban.getText().equals("")||
            namxuatban.getText().equals("")
               ){
            JOptionPane.showMessageDialog(this, "Không được bỏ trống!");
            return;
        }
            
        String ttentacgia = tentacgia.getText();
        String ttengiaotrinh=tengiaotrinh.getText();
        String tnhasanxuat=nhaxuatban.getText();
        Integer tnamxuatban;
        try {
            tnamxuatban= Integer.parseInt(namxuatban.getText());
        } catch(Exception e1){
            JOptionPane.showMessageDialog(this, "Nhập số nguyên !");
            namxuatban.selectAll();
            namxuatban.requestFocus();
            return;
        }
        if (tnamxuatban>2015 || tnamxuatban<1990){
            JOptionPane.showMessageDialog(this, "Nam khong chinh xac !");
            namxuatban.selectAll();
            namxuatban.requestFocus();
            return;
            
        }
        int dem = GiaoTrinhProcess.getCountGiaoTrinh()+1;
        String tmagiaotrinh= "" + dem;
        
        GiaoTrinh ct=new GiaoTrinh(macanbo,ttentacgia,ttengiaotrinh,tnamxuatban,tnhasanxuat,tmagiaotrinh);
        if (giaotrinh == null) { // Them moi sach
        if (GiaoTrinhProcess.insertGiaoTrinh(ct)) {
            JOptionPane.showMessageDialog(this, "Thêm thành công!");
            parent.loadListGiaoTrinh();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Failed!");
        }
    } else {
        //sua sach
            ct.setMaGiaoTrinh(giaotrinh.getMaGiaoTrinh());
        if (GiaoTrinhProcess.updateGiaoTrinh(ct)) {
            JOptionPane.showMessageDialog(this, "Sửa thành công!");
            parent.loadListGiaoTrinh();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Failed!");
        }
    }
        
    }//GEN-LAST:event_okActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
       dispose();
    }//GEN-LAST:event_CancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JLabel jLabel50;
    private javax.swing.JLabel jLabel51;
    private javax.swing.JLabel jLabel56;
    private javax.swing.JLabel jLabel57;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel23;
    private javax.swing.JPanel jPanel24;
    private javax.swing.JTextField namxuatban;
    private javax.swing.JTextField nhaxuatban;
    private javax.swing.JButton ok;
    private javax.swing.JTextField tengiaotrinh;
    private javax.swing.JTextField tentacgia;
    private javax.swing.JLabel txtGiaoTrinh;
    // End of variables declaration//GEN-END:variables
}
